home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GEOFACE / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.0 KB  |  290 lines

  1. /* ==========================================================================
  2.                                FILEIO_C
  3. =============================================================================
  4.  
  5.     FUNCTION NAMES
  6.  
  7.     read_polygon_indices        -- reads the polygon indices file.
  8.     read_polygon_line        -- read the face polyline.
  9.     read_muscles        -- reads the face muscles. 
  10.     read_expression_vectors     -- reads a vector of expressions.
  11.     add_muscle_to_face         -- add a muscle to the face.
  12.  
  13.     C SPECIFICATIONS
  14.  
  15.     read_polygon_indices    ( FileName, face ) 
  16.     read_polygon_line        ( FileName, face )
  17.     read_muscles        ( FileName, face )
  18.     read_expression_vectors    ( FileName, face )
  19.     add_muscle_to_face        ( m, face )
  20.  
  21.     DESCRIPTION
  22.     
  23.     This module is responsible for reading the face data files.  
  24.     This module comes as is with no warranties. 
  25.  
  26.     SIDE EFFECTS
  27.     Unknown.
  28.    
  29.     HISTORY
  30.     Created 16-Dec-94  Keith Waters at DEC's Cambridge Research Lab.
  31.     Modified 22-Nov-96 Sing Bing Kang (sbk@crl.dec.com)
  32.        modified function read_expression_vectors() to allocate
  33.        memory to face->expression (done once)
  34.  
  35. ============================================================================ */
  36.  
  37. #include <math.h>        /* C header for any math functions         */
  38. #include <stdio.h>        /* C header for standard I/O                 */
  39. #include <string.h>        /* For String compare                  */
  40. #include <stdlib.h>
  41. #ifndef _WIN32
  42. #include <sys/types.h>
  43. #include <sys/file.h>
  44. #endif
  45.  
  46. /* 
  47.  * from /usr/include/sys/types.h
  48.  * Just in case TRUE and FALSE are not defined
  49.  */
  50.  
  51. #ifndef TRUE
  52. #define TRUE    1
  53. #endif
  54.  
  55. #ifndef FALSE
  56. #define FALSE   0
  57. #endif
  58.  
  59.  
  60. #include "head.h"        /* local header for the face data structure  */
  61. #include "memory.h"
  62.  
  63. void add_muscle_to_face ( MUSCLE  *m , HEAD    *face );
  64.  
  65. /* ========================================================================= */  
  66. /* read_polygon_indices                                                      */
  67. /* ========================================================================= */  
  68. /*
  69. **   Read in the face data file (x,y,z)
  70. **
  71. */
  72.  
  73. void
  74. read_polygon_indices ( char *FileName, HEAD *face )
  75.  { 
  76.    FILE *InFile ;
  77.    int i, ii ;
  78.  
  79.    /* 
  80.     * Check the FileName.
  81.     */
  82.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  83.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  84.      exit(-1) ;
  85.    }
  86.      
  87.    fscanf ( InFile,"%d", &face->npindices ) ;
  88.  
  89.    /* 
  90.     * Allocate some memory.
  91.     */
  92.    face->indexlist = ( int * ) malloc ( face->npindices*4 * sizeof ( int )) ; 
  93.  
  94.    for( i=0, ii=0; i<face->npindices; i++, ii+=4 )
  95.      fscanf(InFile,"%d%d%d%d", 
  96.         &face->indexlist[ii],   &face->indexlist[ii+1], 
  97.         &face->indexlist[ii+2], &face->indexlist[ii+3] ) ;
  98.         
  99.    fclose( InFile ) ;
  100.    
  101.  }
  102.  
  103. /* ========================================================================= */  
  104. /* read_polygon_line                                                         */
  105. /* ========================================================================= */  
  106. /*
  107. **   Read in the face data file (x,y,z)
  108. **
  109. */
  110.  
  111. void
  112. read_polygon_line ( char *FileName, HEAD *face )
  113. {
  114.   FILE *InFile ;
  115.   int i, ii ;
  116.  
  117.    /* 
  118.     * Check the FileName.
  119.     */
  120.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  121.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  122.      exit(-1) ;
  123.    }
  124.  
  125.   fscanf ( InFile, "%d", &face->npolylinenodes ) ;
  126.  
  127.   /*
  128.    * Allocate some memory.
  129.    */
  130.   face->polyline = ( float * ) malloc ( face->npolylinenodes*3 * sizeof ( float )) ; 
  131.  
  132.   for ( i=0, ii=0; i<face->npolylinenodes; i++, ii+=3 ) {
  133.     
  134.     fscanf ( InFile,"%f%f%f",
  135.         &face->polyline[ii], 
  136.         &face->polyline[ii+1], 
  137.         &face->polyline[ii+2] ) ;
  138.   }
  139.   
  140.   fclose ( InFile ) ;
  141.  
  142. }
  143.  
  144. /* =============================================================
  145.    read_muscles ( FileName, face )
  146.    ========================================================== */
  147. /*
  148. ** This function reads in the muscles.
  149. **
  150. */
  151.  
  152. void
  153. read_muscles ( char *FileName, HEAD *face )
  154. {
  155.   FILE *Infile;
  156.   int i, nm ;
  157.   MUSCLE *m ;
  158.  
  159.   /* 
  160.    * Open the file to be read.
  161.   */
  162.   if((Infile = fopen(FileName,"r")) == 0) {
  163.       fprintf(stderr,"Opening error on file:%10s\n", FileName) ;
  164.       exit(0);
  165.     }
  166.   fscanf ( Infile, "%d", &nm ) ;
  167.  
  168.   for ( i=0; i < nm; i++ ) {
  169.  
  170.       m = _new ( MUSCLE ) ;
  171.  
  172.       fscanf (Infile, "%s %f %f %f %f %f %f %f %f %f %f",
  173.           &(*m->name),
  174.           &m->head[0], &m->head[1], &m->head[2],
  175.           &m->tail[0], &m->tail[1], &m->tail[2],
  176.           &m->fs, &m->fe, &m->zone, &m->clampv ) ;
  177.  
  178.       m->active = FALSE ;
  179.       m->mstat  = 0.0 ;
  180.       
  181.       if (verbose) {
  182.       fprintf(stderr,"%s: %d\n========================\nhx: %2.2f hy: %2.2f hz: %2.2f\ntx: %2.2f ty: %2.2f tz: %2.2f\n fall start: %2.2f\n fall end: %2.2f\n zone: %2.2f\n clampv: %2.2f mstat: %2.2f\n\n",
  183.          m->name, i, 
  184.          m->head[0], 
  185.          m->head[1], 
  186.          m->head[2],
  187.          m->tail[0], 
  188.          m->tail[1],
  189.          m->tail[2],
  190.          m->fs,
  191.          m->fe,
  192.          m->zone,
  193.          m->clampv,
  194.          m->mstat ) ;
  195.       }
  196.  
  197.       add_muscle_to_face ( m, face ) ;
  198.  
  199.     }
  200.  
  201.   fclose(Infile) ;
  202. }
  203.  
  204.  
  205. /* ========================================================================= */  
  206. /* read_expression_vectors                                                   */
  207. /* ========================================================================= */  
  208. /* sbk - added allocated var - 11/22/96 */
  209.  
  210. /*
  211. **   Read in the expression vectors.
  212. */
  213. void
  214. read_expression_vectors ( char *FileName, HEAD *face )
  215. {
  216.   FILE *InFile ;
  217.   int i, k ;
  218.   EXPRESSION *e ;
  219.   static int allocated = 0;
  220.  
  221.    /* 
  222.     * Check the FileName.
  223.     */
  224.    if (( InFile = fopen ( FileName, "r" )) == 0 ) {
  225. #if 0  /* Silently ignore the lack of expression vectors.  I never got the file. -mjk */
  226.      fprintf ( stderr, "can't open input file: %s\n", FileName ) ;
  227. #endif
  228.      face->expression = NULL;
  229.      return;
  230.    }
  231.  
  232.   fscanf ( InFile, "%d", &face->nexpressions ) ;
  233.   fprintf( stderr, "Number of expressions = %d\n", face->nexpressions ) ;
  234.  
  235.   /*
  236.    * Allocate some memory.
  237.    */
  238.   if (!allocated)
  239.     face->expression = (EXPRESSION  **)malloc( face->nexpressions*
  240.                            sizeof(EXPRESSION  *) );
  241.   
  242.   for ( i=0; i<face->nexpressions; i++) {
  243.     if (allocated)
  244.       e = face->expression[i];
  245.     else
  246.       e = face->expression[i] = _new(EXPRESSION) ;
  247.  
  248.     fscanf ( InFile, "%s\n", &(*e->name) ) ;
  249.     
  250.     fprintf ( stderr, "%s\n", e->name ) ;
  251.     
  252.     for ( k=0; k < 17; k++) {
  253.       
  254.       fscanf ( InFile,(k==16) ? "%f\n" : "%f ",   &e->m[k]) ;
  255.       fprintf (stderr,"%2.2f ", e->m[k] ) ;
  256.     }
  257.     fprintf (stderr, "\n") ;
  258.   }
  259.   
  260.   fclose ( InFile ) ;
  261.  
  262.   allocated = 1;
  263. }
  264.  
  265. /* =============================================================== 
  266.    add_muscle_to_face ( m, face )
  267.    =============================================================== */
  268. /*
  269. **   adds a muscle to the face muscle list.
  270. **
  271. */
  272.  
  273. void
  274. add_muscle_to_face ( MUSCLE  *m , HEAD    *face )
  275. {
  276.   int nn ;
  277.  
  278.   if(face->nmuscles == 0)
  279.       face->muscle = _new_array(MUSCLE *, 50) ;
  280.   else if(face->nmuscles % 50 == 0)
  281.       face->muscle = _resize_array(face->muscle,MUSCLE *,face->nmuscles+50) ;
  282.  
  283.   nn = face->nmuscles ;
  284.   face->muscle[nn] = m ;
  285.  
  286.   face->nmuscles++ ;
  287.  
  288. }
  289.  
  290.